// fn main () {
//     let s  = dangle();
// }
//
// fn dangle() -> &str {
//     "".to_owned()
// }



/// 长方形结构体
///
/// # Examples
/// ```rust
/// let rect = Rectangle{width:30, height:40};
/// println!("area {}", rect.area);
/// ```
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}



impl Rectangle {
    /// 实现结构体的方法
    ///
    /// 可以使用多个代码块来实现具体方法
    /// # Examples
    /// ```rust
    /// impl Rectangle{
    /// /// block first
    /// /// ... methods
    /// }
    ///
    /// impl Rectangle{
    /// /// block second
    /// /// ... methods
    /// }
    /// ```
    /// 第一个参数必须是结构体本身
    ///
    /// 可以是 `&self` 或 `self: &Self` ,`&self` 是 `self: &self` 的缩写
    fn area(self: &Self) -> u64 {
        self.width as u64 * self.height as u64
    }

    /// 反转高宽
    ///
    /// 修改属性时必须设置结构指针为可变指针，不然无法修改
    /// # Examples
    /// ```rust
    /// let mut rect = Rectangle{width:30, height:40};
    /// rect.rev();
    /// dbg!(rect);
    /// ```
    fn rev(&mut self) {
        let tmp = self.width;
        self.width = self.height;
        self.height = tmp;
    }

    fn crop(&mut self) {}

    /// 是否可以hold住
    ///
    /// 多参数方法，将其余参数写到`&self`后面即可
    /// # Examples
    /// ```rust
    /// let rect1 = Rectangle{width:300, height:300}
    /// let rect2 = Rectangle{width:200, height:200}
    /// let rect3 = Rectangle{width:400, height:400}
    /// println!("rect1 hold rect2 {}", rect1.can_hold(rect2));
    /// println!("rect1 hold rect3 {}", rect1.can_hold(rect3));
    /// ```
    /// # Output
    /// ```
    /// rect1 hold rect2 true
    /// rect2 hold rect2 false
    /// ```
    fn can_hold(&self, hold_rect: &Rectangle) -> bool {
        self.width > hold_rect.width && self.height > hold_rect.height
    }

    /// 静态方法
    ///
    /// 第一个参数不会传递 `&self`
    /// # Examples
    ///
    /// ```rust
    /// let s = Rectangle::square(30);
    /// println!("{:#?}", s)
    /// ```
    /// # Output
    /// ```
    /// Rectangle {
    ///     width: 30,
    ///     height: 30,
    /// }
    /// ```
    fn square(size: u32) -> Self {
        Self {
            width: size,
            height: size,
        }
    }

    /// 判断是否正方形
    ///
    /// 静态方法通常用于实例化，但不仅限于实例化
    ///
    /// # Examples
    /// ```rust
    /// let s = Rectangle::square(30);
    /// println!("rect is square {}", Rectangle::is_square(&s));
    /// ```
    /// # Output
    /// ```
    /// rect is square true
    /// ```
    fn is_square(rect: &Rectangle) -> bool {
        rect.width == rect.height
    }
}

fn main() {
    let s = Rectangle::square(30);
    println!("rect is square {}", Rectangle::is_square(&s));
    println!("{:#?}", s);


}
